package com.bysj.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.bysj.model.Comment;
import com.bysj.model.Reply;
import com.bysj.utils.DBUtil;

public class CommentDao {

	public Comment getOrderComment(int orderId) throws SQLException {
		QueryRunner r = new QueryRunner(DBUtil.getDataSource());
		String sql = "select id,order_id as orderId,username,content,createtime,user_id as userId from comment where order_id = ?";
		return r.query(sql, new BeanHandler<Comment>(Comment.class), orderId);
	}

	public Reply getCommentReply(int commId) throws SQLException {
		QueryRunner r = new QueryRunner(DBUtil.getDataSource());
		String sql = "select id,order_id as orderId,comm_id as commId,username,content,createtime,user_id as userId from reply where order_id = ?";
		return r.query(sql, new BeanHandler<Reply>(Reply.class), commId);
	}

	public void addComment(Comment vo) throws SQLException {
		QueryRunner r = new QueryRunner(DBUtil.getDataSource());
		String sql = "insert into comment(order_id,username,content,user_id,createtime) values(?,?,?,?,now())";
		r.update(sql, vo.getOrderId(), vo.getUsername(), vo.getContent(), vo.getUserId());
	}

	public void addReply(Reply vo) throws SQLException {
		QueryRunner r = new QueryRunner(DBUtil.getDataSource());
		String sql = "insert into reply(order_id,comm_id,username,content,user_id,createtime) values(?,?,?,?,?,now())";
		r.update(sql, vo.getOrderId(), vo.getCommId(), vo.getUsername(), vo.getContent(), vo.getUserId());
	}
}
